package com.zkzx.operation.api.service.authorization;

import com.zkzx.common.global.Global;
import com.zkzx.operation.api.mapper.RoleAuthorityMapper;
import com.zkzx.operation.api.mapper.entity.RoleAuthority;
import com.zkzx.operation.api.mapper.entity.RoleAuthorityExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月27日 17时26分
 */
@Service
public class RoleAuthorityService {

    @Resource
    RoleAuthorityMapper roleAuthorityMapper;

    /**
     * 根据角色id获取对应的权限
     * @param roleId 角色
     * @return 权限集合
     */
    public List<RoleAuthority> getByRoleId(Integer roleId){

        // 封装用户的权限信息
        RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
        roleAuthorityExample.createCriteria().andRoleidEqualTo(roleId);
        return roleAuthorityMapper.selectByExample(roleAuthorityExample);
    }

    /**
     * 根据角色 和 层级 查询权限菜单
     * @param roleId 角色id
     * @param level 等级id
     * @param isMenu 是否查询菜单
     * @return 权限列表
     */
    public List<RoleAuthority> getByRoleIdAndLevelAndIsMenu(Integer roleId, Integer level, boolean isMenu){

        // 封装用户的权限信息
        RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
        roleAuthorityExample.createCriteria().andRoleidEqualTo(roleId).andLevelEqualTo(level).andIsmenuEqualTo((byte) (isMenu ? 0x01 : 0x00));
        return roleAuthorityMapper.selectByExample(roleAuthorityExample);
    }

    /**
     * 校验角色和权限是否匹配
     * @param roleId 角色
     * @param authorityId 权限
     * @return 是否匹配
     */
    public boolean checkRoleAuthority(Integer roleId, Integer authorityId){
        RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
        roleAuthorityExample.createCriteria().andRoleidEqualTo(roleId).andAuthorityidEqualTo(authorityId);
        return roleAuthorityMapper.countByExample(roleAuthorityExample) > Global.ZERO;
    }
}
